package com.tt;

import java.util.ArrayList;
import java.util.Random;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 * @author terry
 * @date 2021/5/15
 */
public class EquationTestingApplication5 {

    private static Random random = new Random();

    public static void main(String[] args) {
//        int unknownsCount = 2;
//        int questionsCount = 10;
        
        printLinearEquation(2, 4);
        printLinearEquation(3, 4);
    }

    /**
     * 生成方程组
     * @param unknownsCount 未知数个数
     * @param total 题目数量
     */
    private static void printLinearEquation(int unknownsCount, int total){
        for(int i = 0; i< total; i++){
            String str = createLinearEquation(unknownsCount);
            System.out.println(str);
            System.out.println();
        }
    }

    private static String createLinearEquation(int unknownsCount){
        List<String> list = new ArrayList<>();

        for(int i = 0; i < unknownsCount; i++) {
            String str = createOneLinearEquation(unknownsCount);
            list.add(str);
        }
        return StringUtils.join(list, "\n");
    }

    private static String createOneLinearEquation(int unknownsCount){
        StringBuilder sb = new StringBuilder();
        int pos = 0;
        char unknownChar = 'x';
        for(int i = 0; i < unknownsCount; i++) {
            String term = createTerm((char)((int)unknownChar + i));
            if (pos > 0){
                sb.append(randomOperator());
            }
            sb.append(term);
            pos++;
        }
        sb.append(" = ").append(randomInt4Answer());
        return sb.toString();
    }

    private static String createTerm(char unknownChar){
        StringBuilder sb = new StringBuilder();

        boolean needBracket = randomBoolean(); // 是否生成括号
        int bracketFactor = randomScale(); // 括号系数
        int unknownCharFactor = randomScale(); // 未知数系数
        int constantTerm = randomInt(); // 常数项

        if (needBracket){
            sb.append("(");
            if (bracketFactor != 1){
                sb.insert(0, bracketFactor);
            }
        }
        if (unknownCharFactor != 1){
            sb.append(unknownCharFactor);
        }
        sb.append(unknownChar);
        if (needBracket){
            sb.append(randomOperator());
            sb.append(constantTerm);
            sb.append(")");
        }
        
        return sb.toString();
    }

    private static boolean randomBoolean(){
        return random.nextBoolean();
    }

    private static int randomUnit(){
        int i = 0;
        do {
            i = random.nextInt(10);
        }
        while (i==0);

        return i;
    }

    private static int randomInt(){
        int i = 0;
        do {
            i = random.nextInt(5);
        }
        while (i==0);
        
        return i;
    }

    private static int randomScale(){
        int i = 0;
        do {
            i = random.nextInt(3);
        }
        while (i==0);

        return i;
    }

    private static int randomInt4Answer(){
        return randomUnit() + 10;
    }

    private static String randomOperator(){
        boolean positive = randomBoolean();
        return positive ? " + " : " - ";
    }
}
